﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WindowHeight = 35;
            byte size = 30;
            Random rand = new Random();
            byte[,] population = new byte[size+2, size+2];
            ConsoleKeyInfo key;
            byte count = 0;
            //Generation
            Console.WriteLine("Evolution");
            Console.WriteLine();
            for (int i = 1; i < size+1; i++)
            {
                for (int j = 1; j < size+1; j++)
                {
                    population[i, j] = Convert.ToByte(Math.Round(rand.NextDouble()));
                    if (population[i, j] == 1)
                        Console.Write(" *");
                    else Console.Write("  ");
                }
                Console.WriteLine();
            }
            //Evolution

            key = Console.ReadKey();
            Console.WriteLine();
            while (key.Key == ConsoleKey.Spacebar) 
            {
                //next evolution
                for (int i = 1; i < size+1; i++)
                    for (int j = 1; j < size+1; j++)
                        if (population[i, j] == 0)
                        {
                            count = 0;
                            for (int m = i - 1; m <= i + 1; m++)
                                for (int n = j - 1; n <= j + 1; n++)
                                    if ((population[m, n] == 1) && ((m == i) || (n == j))) count++;
                            if (count > 1) population[i, j] = 1;
                        }
                        else
                        {
                            count = 0;
                            for (int m = i - 1; m <= i + 1; m++)
                                for (int n = j - 1; n <= j + 1; n++)
                                    if ((population[m, n] == 0) && ((m == i) || (n == j))) count++;
                            if (count == 0) population[i, j] = 0;
                        }

                //Output
                Console.Clear();
                Console.WriteLine("Evolution");
                Console.WriteLine();
                for (int i = 1; i < size+1; i++)
                {
                    for (int j = 1; j < size+1; j++)
                        if (population[i, j]==1) 
                        Console.Write(" *");
                        else Console.Write("  ");
                    Console.WriteLine();
                }
                   key = Console.ReadKey(); 
            } 
             //Console.ReadLine();
        }
    }
}
